package io.renren.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Maps;

import io.renren.dao.NationalFlagDao;
import io.renren.entity.NationalFlagEntity;
import io.renren.service.NationalFlagService;
import io.renren.utils.ShortUrl;



@Service("nationalFlagService")
public class NationalFlagServiceImpl implements NationalFlagService {
	@Autowired
	private NationalFlagDao nationalFlagDao;
	
	@Value("${file.path:#{null}}")
    private String filePath;
	
	@Override
	public NationalFlagEntity queryObject(Integer id){
		return nationalFlagDao.queryObject(id);
	}
	
	@Override
	public List<NationalFlagEntity> queryList(Map<String, Object> map){
		return nationalFlagDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return nationalFlagDao.queryTotal(map);
	}
	
	@Override
	public void save(NationalFlagEntity nationalFlag){
		nationalFlagDao.save(nationalFlag);
	}
	
	@Override
	public void update(NationalFlagEntity nationalFlag){
		nationalFlagDao.update(nationalFlag);
	}
	
	@Override
	public void delete(Integer id){
		deleteOldFile(id);
		nationalFlagDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		for(Integer id : ids) {
			deleteOldFile(id);
		}
		nationalFlagDao.deleteBatch(ids);
	}
	
	private void deleteOldFile(Integer id) {
		NationalFlagEntity old = nationalFlagDao.queryObject(id);
		if(old != null) {
			File f = new File(old.getFilePath());
			if(f.exists()) f.delete();
		}
	}

	@Override
	public void upload(MultipartFile file, NationalFlagEntity nationalFlag) throws IllegalStateException, IOException {
		NationalFlagEntity old = nationalFlagDao.queryByName(file.getOriginalFilename());
		if(old != null) nationalFlagDao.delete(old.getId());
		NationalFlagEntity entity = new NationalFlagEntity();
		String fileName = file.getOriginalFilename();
		entity.setCountryName(nationalFlag.getCountryName());
		entity.setFileName(fileName);
		entity.setSize(new Long(file.getSize()).intValue());
		entity.setRemark(nationalFlag.getRemark());
		
		String folderPath = filePath+ "national_flag\\";
		File folder = new File(folderPath);
		if(!folder.isDirectory()) folder.mkdir();
		
		String path = filePath+ "national_flag\\" + file.getOriginalFilename();
		entity.setFilePath(path);
		File f = new File(path);
		if(f.exists()) f.delete();
		entity.setShortUrl(ShortUrl.getShortUrl(file.getOriginalFilename()));
		file.transferTo(new File(path));
		save(entity);
	}
	
	public static void main(String[] args) {
		File file = new File("D:\\personal\\签亿国际\\国旗\\png\\");
		
		File[] arr = file.listFiles();
		for(int i=0; i<arr.length; i++) {
			File f = arr[i];
			System.out.println((i + 1) + "," +  f.getName().split("\\.")[0] + "," + f.getName() + ",D:\\img\\national_flag" + "," + ShortUrl.getShortUrl(f.getName()) + "," + f.length());
		}
	}

	@Override
	public NationalFlagEntity queryByUrl(String url) {
		return nationalFlagDao.queryByUrl(url);
	}

	@Override
	public List<NationalFlagEntity> query(Map<String, Object> params) {
		return nationalFlagDao.query(params);
	}
	
}
